import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'hash',
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
    },
    // 其他设置模块
    {
      path: '/setting',
      redirect: '/setting/position',
      component: () => import(/* webpackChunkName: "setting" */ '../views/SettingPages/IndexView.vue'),
      name: 'setting',
      meta: {
        // 页面标题title
        name: 'setting',
      },
      children: [
        {
          path: 'position',
          name: 'position',
          component: () => import(/* webpackChunkName: "position" */ '../views/SettingPages/PositionSetting.vue')
        },
        {
          path: 'netWork',
          name: 'netWork',
          component: () => import(/* webpackChunkName: "netWork" */ '../views/SettingPages/NetWorkSetting.vue')
        },
      ],
    },
    // 车缝模块
    {
      path: '/sewing',
      redirect: '/sewing/sewingHome',
      component: () => import(/* webpackChunkName: "sewing" */ '../views/SewingPages/IndexView.vue'),
      name: 'sewing',
      meta: {
        // 页面标题title
        name: 'sewing',
      },
      children: [
        {
          path: 'sewingHome',
          name: 'sewingHome',
          component: () => import(/* webpackChunkName: "sewingHome" */ '../views/SewingPages/SewingHome.vue')
        },
        {
          path: 'sewingHomeTwo',
          name: 'sewingHomeTwo',
          component: () => import(/* webpackChunkName: "sewingHomeTwo" */ '../views/SewingPages/SewingHomeTwo.vue')
        },
        {
          path: 'planList',
          name: 'planList',
          component: () => import(/* webpackChunkName: "planList" */ '../views/SewingPages/PlanList.vue')
        }
      ],
    },
    // 挂片模块
    {
      path: '/hanger',
      redirect: '/hanger/hangerHome',
      component: () => import(/* webpackChunkName: "hanger" */ '../views/HangerPages/IndexView.vue'),
      name: 'hanger',
      meta: {
        // 页面标题title
        name: 'hanger',
      },
      children: [
        {
          path: 'hangerHome',
          name: 'hangerHome',
          component: () => import(/* webpackChunkName: "hangerHome" */ '../views/HangerPages/HangerHome.vue')
        },
        {
          path: 'clothingList',
          name: 'clothingList',
          component: () => import(/* webpackChunkName: "clothingList" */ '../views/HangerPages/ClothingList.vue')
        },
        {
          path: 'startTask',
          name: 'startTask',
          component: () => import(/* webpackChunkName: "startTask" */ '../views/HangerPages/StartTask.vue')
        },
      ],
    },
  ],
})

router.beforeEach(function (to, from, next) {
  if (to.query.token) {
    sessionStorage.setItem('token', to.query.token)
  }
  if (to.path === '/home') {
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      next({
        name: 'MES_LOGIN',
        query: to.query, //登录后再跳回此页面时要做的配置
      })
    }
  }
  // 判断是否需要登录
  else if (to.meta.needLogin) {
    // 从本地中获取用户信息，判断是否需要
    if (sessionStorage.getItem('token')) {
      next() //表示已登录
    } else {
      //未登录
      next({
        name: 'MES_LOGIN',
        query: to.query, //登录后再跳回此页面时要做的配置
      })
    }
  } else {
    // 不需要登录
    next()
  }
})

export default router
